// EditCameraDlg.cpp : implementation file
//
#pragma warning(disable:4786)

#include "stdafx.h"
#include "RMSTranslator.h"
#include "EditCameraDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "RMSTranslatorDlg.h"
#define GET_MAIN_DLG			((CRMSTranslatorDlg*)AfxGetMainWnd())
#define getHomePath				GET_MAIN_DLG->getHomePath
#define saveHomePath			GET_MAIN_DLG->saveHomePath

/////////////////////////////////////////////////////////////////////////////
// CEditCameraDlg dialog


CEditCameraDlg::CEditCameraDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEditCameraDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditCameraDlg)
	m_nBitrate = 500;
	m_strBuilding = _T("");
	m_strCamera = _T("");
	m_nCameraID = 0;
	m_nPort = 777;
	m_nFrameRate = 15;
	m_nHeight = 240;
	m_nQuality = 0;
	m_strRoom = _T("");
	m_strSite = _T("");
	m_nWidth = 320;
	m_strPassword = _T("");
	//}}AFX_DATA_INIT
}


void CEditCameraDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditCameraDlg)
	DDX_Control(pDX, IDC_PASSWORD, m_editPassword);
	DDX_Control(pDX, IDSUBMIT, m_btnSubmit);
	DDX_Control(pDX, IDDELETE, m_btnDelete);
	DDX_Control(pDX, IDCLEAR, m_btnClear);
	DDX_Control(pDX, IDADDCAMERA, m_btnAddCamera);
	DDX_Control(pDX, IDC_WIDTH, m_editWidth);
	DDX_Control(pDX, IDC_QUALITY, m_editQuality);
	DDX_Control(pDX, IDC_HEIGHT, m_editHeight);
	DDX_Control(pDX, IDC_FRAMERATE, m_editFrameRate);
	DDX_Control(pDX, IDC_CAMERAPORT, m_editPort);
	DDX_Control(pDX, IDC_CAMERAID, m_editCameraID);
	DDX_Control(pDX, IDC_BITRATE, m_editBitrate);
	DDX_Control(pDX, IDC_SITE, m_cmbxSite);
	DDX_Control(pDX, IDC_ROOM, m_cmbxRoom);
	DDX_Control(pDX, IDC_CAMERA, m_cmbxCamera);
	DDX_Control(pDX, IDC_BUILDING, m_cmbxBuilding);
	DDX_Control(pDX, IDC_CAMERAIP, m_ipCamera);
	DDX_Text(pDX, IDC_BITRATE, m_nBitrate);
	DDV_MinMaxUInt(pDX, m_nBitrate, 0, 1000);
	DDX_CBString(pDX, IDC_BUILDING, m_strBuilding);
	DDX_CBString(pDX, IDC_CAMERA, m_strCamera);
	DDX_Text(pDX, IDC_CAMERAID, m_nCameraID);
#ifdef _J707_ // merge from VC-XR2:#103R210009:KAZUHY
	DDV_MinMaxUInt(pDX, m_nCameraID, 0, 100000);
#else
	DDV_MinMaxUInt(pDX, m_nCameraID, 0, 10000);
#endif
	DDX_Text(pDX, IDC_CAMERAPORT, m_nPort);
	DDV_MinMaxUInt(pDX, m_nPort, 500, 65000);
	DDX_Text(pDX, IDC_FRAMERATE, m_nFrameRate);
	DDV_MinMaxUInt(pDX, m_nFrameRate, 1, 30);
	DDX_Text(pDX, IDC_HEIGHT, m_nHeight);
	DDV_MinMaxUInt(pDX, m_nHeight, 240, 240);
	DDX_Text(pDX, IDC_QUALITY, m_nQuality);
	DDV_MinMaxUInt(pDX, m_nQuality, 0, 10);
	DDX_CBString(pDX, IDC_ROOM, m_strRoom);
	DDX_CBString(pDX, IDC_SITE, m_strSite);
	DDX_Text(pDX, IDC_WIDTH, m_nWidth);
	DDV_MinMaxUInt(pDX, m_nWidth, 320, 320);
	DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
	DDV_MaxChars(pDX, m_strPassword, 128);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEditCameraDlg, CDialog)
	//{{AFX_MSG_MAP(CEditCameraDlg)
	ON_CBN_EDITCHANGE(IDC_SITE, OnEditChangeSite)
	ON_CBN_EDITCHANGE(IDC_BUILDING, OnEditChangeBuilding)
	ON_CBN_EDITCHANGE(IDC_ROOM, OnEditChangeRoom)
	ON_CBN_EDITCHANGE(IDC_CAMERA, OnEditChangeCamera)
	ON_CBN_SELENDOK(IDC_SITE, OnSelendokSite)
	ON_CBN_SELENDOK(IDC_BUILDING, OnSelendokBuilding)
	ON_CBN_SELENDOK(IDC_ROOM, OnSelendokRoom)
	ON_CBN_SELENDOK(IDC_CAMERA, OnSelendokCamera)
	ON_BN_CLICKED(IDADDCAMERA, OnAddcamera)
	ON_BN_CLICKED(IDCLEAR, OnClear)
	ON_EN_CHANGE(IDC_BITRATE, EnableSubmit)
	ON_BN_CLICKED(IDDELETE, OnDelete)
	ON_BN_CLICKED(IDSUBMIT, OnSubmit)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_CAMERAIP, OnFieldchangedCameraip)
	ON_EN_KILLFOCUS(IDC_BITRATE, OnKillfocus)
	ON_EN_CHANGE(IDC_CAMERAID, EnableSubmit)
	ON_EN_CHANGE(IDC_CAMERAPORT, EnableSubmit)
	ON_EN_CHANGE(IDC_FRAMERATE, EnableSubmit)
	ON_EN_CHANGE(IDC_HEIGHT, EnableSubmit)
	ON_EN_CHANGE(IDC_QUALITY, EnableSubmit)
	ON_EN_CHANGE(IDC_WIDTH, EnableSubmit)
	ON_EN_KILLFOCUS(IDC_CAMERAID, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_CAMERAPORT, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_FRAMERATE, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_HEIGHT, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_QUALITY, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_WIDTH, OnKillfocus)
	ON_EN_CHANGE(IDC_PASSWORD, EnableSubmit)
	ON_EN_KILLFOCUS(IDC_PASSWORD, OnKillfocus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditCameraDlg message handlers
BOOL CEditCameraDlg::OnInitDialog() 
{
	if(!pBaseServer) 
	{
		EndDialog(0);
		return FALSE;
	}

	CDialog::OnInitDialog();

	m_ipCamera.SetAddress(127,0,0,1);
	UpdateData(FALSE);

	m_cmbxSite.ResetContent();
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	m_cmbxBuilding.EnableWindow(FALSE);
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);

	m_bEnableSubmit = FALSE;

	SetCurState(FALSE);

	map<CString,CSite>::const_iterator s;
/*	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;
	map<CString,CInfo>::const_iterator c;

	for(s = pBaseServer->m_mapInfo.begin(); s != pBaseServer->m_mapInfo.end(); s++)
	{
		for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
		{
			for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
			{
				for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
				{
					m_mapInfoTemp[s->first].m_Buildings[b->first].m_Rooms[r->first].m_Cameras[c->first] = c->second;
				}
			}
		}
	}*/

	m_mapInfoTemp = pBaseServer->GetMapInfo();

	m_cmbxSite.AddString("none");
	for(s = m_mapInfoTemp.begin(); s != m_mapInfoTemp.end(); ++s)
	{
		m_cmbxSite.AddString((LPCTSTR)s->first);
	}

	m_cmbxSite.SetCurSel(m_cmbxSite.FindStringExact(-1, "none"));

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CEditCameraDlg::SetCurState(BOOL bState, BOOL bEdit)
{
	m_editBitrate.EnableWindow(bState);
	m_editCameraID.EnableWindow(bState);
	m_editFrameRate.EnableWindow(bState);
	m_editHeight.EnableWindow(bState);
	m_editWidth.EnableWindow(bState);
	m_editPort.EnableWindow(bState);
	m_editQuality.EnableWindow(bState);
	m_ipCamera.EnableWindow(bState);
	m_editPassword.EnableWindow(bState);

	if(bState)
	{
		if(bEdit)
		{
			m_btnAddCamera.EnableWindow(FALSE);
			m_btnClear.EnableWindow(FALSE);
			m_btnDelete.EnableWindow(TRUE);

			m_bEdit = TRUE;
		}
		else
		{
			m_btnAddCamera.EnableWindow(TRUE);
			m_btnClear.EnableWindow(TRUE);
			m_btnDelete.EnableWindow(FALSE);

			m_bEdit = FALSE;
		}
	}
	else
	{
		m_btnAddCamera.EnableWindow(FALSE);
		m_btnClear.EnableWindow(FALSE);
		m_btnDelete.EnableWindow(FALSE);
	}

	if(m_bEnableSubmit)
	{
		m_btnSubmit.EnableWindow(TRUE);
	}
	else
	{
		m_btnSubmit.EnableWindow(FALSE);
	}
}



void CEditCameraDlg::OnEditChangeSite() 
{
	UpdateData(TRUE);
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	if((m_strSite != "") && (m_strSite != "none"))
	{
		m_cmbxBuilding.EnableWindow(TRUE);
		int n;
		if((n = m_cmbxSite.FindStringExact(-1,m_strSite)) != CB_ERR)
		{
			m_cmbxSite.SetCurSel(n);
			SetCurSite();
		}
		else
		{
			m_cmbxBuilding.AddString("none");
			m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxBuilding.EnableWindow(FALSE);
	}
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CEditCameraDlg::OnEditChangeBuilding() 
{
	UpdateData(TRUE);

	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();
	if((m_strBuilding != "") && (m_strBuilding != "none"))
	{
		m_cmbxRoom.EnableWindow(TRUE);

		int n;
		if((n = m_cmbxBuilding.FindStringExact(-1,m_strBuilding)) != CB_ERR)
		{
			m_cmbxBuilding.SetCurSel(n);
			SetCurBuilding();
		}
		else
		{
			m_cmbxRoom.AddString("none");
			m_cmbxRoom.SetCurSel(m_cmbxRoom.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxRoom.EnableWindow(FALSE);
	}
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CEditCameraDlg::OnEditChangeRoom() 
{
	UpdateData(TRUE);

	m_cmbxCamera.ResetContent();
	if((m_strRoom != "") && (m_strRoom != "none"))
	{
		m_cmbxCamera.EnableWindow(TRUE);

		int n;
		if((n = m_cmbxRoom.FindStringExact(-1,m_strRoom)) != CB_ERR)
		{
			m_cmbxRoom.SetCurSel(n);
			SetCurRoom();
		}
		else
		{
			m_cmbxCamera.AddString("none");
			m_cmbxCamera.SetCurSel(m_cmbxCamera.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxCamera.EnableWindow(FALSE);
	}
	SetCurState(FALSE);
}

void CEditCameraDlg::OnEditChangeCamera() 
{
	UpdateData(TRUE);

	if((m_strCamera != "") && (m_strCamera != "none"))
	{
		int n;
		if((n = m_cmbxCamera.FindStringExact(-1,m_strCamera)) != CB_ERR)
		{
			m_cmbxCamera.SetCurSel(n);
			SetCurCamera();

			m_bEnableSubmit = TRUE;
			SetCurState(TRUE, TRUE);
		}
		else
		{
			SetCurState(TRUE, FALSE);
		}
	}
	else
	{
		SetCurState(FALSE);
	}
}

void CEditCameraDlg::OnSelendokSite() 
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurSite();

	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CEditCameraDlg::OnSelendokBuilding() 
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurBuilding();

	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CEditCameraDlg::OnSelendokRoom() 
{
	m_cmbxCamera.ResetContent();

	SetCurRoom();

	SetCurState(FALSE);
}

void CEditCameraDlg::OnSelendokCamera() 
{
	SetCurCamera();
}

void CEditCameraDlg::SetCurSite()
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	int nCurrentSite = m_cmbxSite.GetCurSel();
	CString site;
	
	m_cmbxSite.GetLBText(nCurrentSite, site);

	m_strSite = site;
	UpdateData(FALSE);
	
	if(site == "none")
	{
		m_cmbxBuilding.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CBuilding>::const_iterator b;

		CSite* pCurrentSite;
		pCurrentSite = &(m_mapInfoTemp[site]);

		m_cmbxBuilding.AddString("none");
		for(b = pCurrentSite->m_Buildings.begin(); b != pCurrentSite->m_Buildings.end(); ++b)
		{
			m_cmbxBuilding.AddString((LPCTSTR)b->first);
		}
		m_cmbxBuilding.EnableWindow(TRUE);
		m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindStringExact(-1, "none"));
	}
}

void CEditCameraDlg::SetCurBuilding()
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
	CString building;
	
	m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

	m_strBuilding = building;
	UpdateData(FALSE);

	if(building == "none")
	{
		m_cmbxRoom.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CRoom>::const_iterator r;

		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;
		
		m_cmbxSite.GetLBText(nCurrentSite, site);
		CSite* pCurrentSite;
		pCurrentSite = &(m_mapInfoTemp[site]);
		
		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		m_cmbxRoom.AddString("none");
		for(r = pCurrentBuilding->m_Rooms.begin(); r != pCurrentBuilding->m_Rooms.end(); ++r)
		{
			m_cmbxRoom.AddString((LPCTSTR)r->first);
		}
		m_cmbxRoom.EnableWindow(TRUE);
		m_cmbxRoom.SetCurSel(m_cmbxRoom.FindStringExact(-1, "none"));
	}
}

void CEditCameraDlg::SetCurRoom()
{
	m_cmbxCamera.ResetContent();

	int nCurrentRoom = m_cmbxRoom.GetCurSel();
	CString room;

	m_cmbxRoom.GetLBText(nCurrentRoom, room);

	m_strRoom = room;
	UpdateData(FALSE);

	if(room == "none")
	{
		m_cmbxCamera.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CInfo>::const_iterator c;

		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;
		
		m_cmbxSite.GetLBText(nCurrentSite, site);
		CSite* pCurrentSite;
		pCurrentSite = &(m_mapInfoTemp[site]);

		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		CString building;
		
		m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		CRoom* pCurrentRoom = &(pCurrentBuilding->m_Rooms[room]);

		m_cmbxCamera.AddString("none");
		for(c = pCurrentRoom->m_Cameras.begin(); c != pCurrentRoom->m_Cameras.end(); ++c)
		{
			m_cmbxCamera.AddString((LPCTSTR)c->first);
		}

		m_cmbxCamera.EnableWindow(TRUE);
		m_cmbxCamera.SetCurSel(m_cmbxCamera.FindStringExact(-1, "none"));
	}
}

void CEditCameraDlg::SetCurCamera()
{
	int nCurrentCamera = m_cmbxCamera.GetCurSel();
	CString camera;
	
	m_cmbxCamera.GetLBText(nCurrentCamera, camera);
	
	if(camera != "none")
	{
		int nCurrentSite = m_cmbxSite.GetCurSel();
		CString site;
		m_cmbxSite.GetLBText(nCurrentSite, site);

		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		CString building;
		m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

		int nCurrentRoom = m_cmbxRoom.GetCurSel();
		CString room;
		m_cmbxRoom.GetLBText(nCurrentRoom, room);

		UpdateData(TRUE);

		m_strCamera = camera;

		CInfo& cam = m_mapInfoTemp[site].
						m_Buildings[building].
							m_Rooms[room].
								m_Cameras[camera];

		DWORD inet_address = cam.getAddress();
		BYTE* pbIP = (BYTE*)&inet_address;
		m_nCameraID = cam.m_iID;
		m_strPassword = cam.m_strPassword;
		if (m_strPassword=="") m_strPassword="none";
		m_ipCamera.SetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);
		m_nPort = cam.m_port;
		m_nFrameRate = cam.m_iFrameRate;
		m_nBitrate = cam.m_iBitrate;
		m_nQuality = cam.m_iQualityRange;
		m_nHeight =	cam.m_iHeight;
		m_nWidth = cam.m_iWidth;
		UpdateData(FALSE);
		
		SetCurState(TRUE, TRUE);
	}
	else
	{
		UpdateData(TRUE);

		m_strCamera = camera;

		m_nBitrate = 500;
		m_nCameraID = 0;
		m_nPort = 777;
		m_nFrameRate = 15;
		m_nHeight = 240;
		m_nQuality = 0;
		m_nWidth = 320;
		m_ipCamera.SetAddress(127,0,0,1);
		m_strPassword = "none";
		UpdateData(FALSE);

		SetCurState(FALSE);
	}
}

void CEditCameraDlg::OnAddcamera() 
{
	UpdateData(TRUE);

	in_addr inet_address;
	BYTE* pbIP = (BYTE*)&inet_address.S_un.S_addr;
	m_ipCamera.GetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);

	CInfo& cam = m_mapInfoTemp[m_strSite].
					m_Buildings[m_strBuilding].
						m_Rooms[m_strRoom].
							m_Cameras[m_strCamera];
	if (m_strPassword=="") m_strPassword="none";
	cam.setPassword(m_strPassword);
	cam.setAddress(inet_address.S_un.S_addr);
	cam.m_iID = m_nCameraID;
	cam.m_port = m_nPort;
	cam.m_iFrameRate = m_nFrameRate;
	cam.m_iBitrate = m_nBitrate;
	cam.m_iQualityRange = m_nQuality;
	cam.m_iHeight = m_nHeight;
	cam.m_iWidth = m_nWidth;

	if(m_cmbxSite.FindStringExact(-1,m_strSite) == CB_ERR)
	{
		m_cmbxSite.AddString(m_strSite);
	}
	if(m_cmbxBuilding.FindStringExact(-1,m_strBuilding) == CB_ERR)
	{
		m_cmbxBuilding.AddString(m_strBuilding);
	}
	if(m_cmbxRoom.FindStringExact(-1,m_strRoom) == CB_ERR)
	{
		m_cmbxRoom.AddString(m_strRoom);
	}
	if(m_cmbxCamera.FindStringExact(-1,m_strCamera) == CB_ERR)
	{
		m_cmbxCamera.AddString(m_strCamera);
	}

	m_bEnableSubmit = TRUE;
	SetCurState(TRUE, TRUE);
}

void CEditCameraDlg::OnClear() 
{
	UpdateData(TRUE);
	m_nBitrate = 500;
	m_nCameraID = 0;
	m_nPort = 777;
	m_nFrameRate = 15;
	m_nHeight = 240;
	m_nQuality = 0;
	m_nWidth = 320;
	m_ipCamera.SetAddress(127,0,0,1);
	m_strPassword = "none";
	UpdateData(FALSE);
}

void CEditCameraDlg::EnableSubmit() 
{
	if(m_bEdit)
	{
		m_bEnableSubmit = TRUE;
		SetCurState(TRUE, TRUE);
	}
}

void CEditCameraDlg::OnDelete() 
{
	int nLen;

	int nCurrentSite = m_cmbxSite.GetCurSel();
	CString site;
	nLen = m_cmbxSite.GetLBTextLen(nCurrentSite);
	m_cmbxSite.GetLBText(nCurrentSite, site.GetBuffer(nLen));
	site.ReleaseBuffer();

	int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
	CString building;
	nLen = m_cmbxBuilding.GetLBTextLen(nCurrentBuilding);
	m_cmbxBuilding.GetLBText(nCurrentBuilding, building.GetBuffer(nLen));
	building.ReleaseBuffer();

	int nCurrentRoom = m_cmbxRoom.GetCurSel();
	CString room;
	nLen = m_cmbxRoom.GetLBTextLen(nCurrentRoom);
	m_cmbxRoom.GetLBText(nCurrentRoom, room.GetBuffer(nLen));
	room.ReleaseBuffer();
	
	int nCurrentCamera = m_cmbxCamera.GetCurSel();
	CString camera;
	nLen = m_cmbxCamera.GetLBTextLen(nCurrentCamera);
	m_cmbxCamera.GetLBText(nCurrentCamera, camera.GetBuffer(nLen));
	camera.ReleaseBuffer();

	m_mapInfoTemp[m_strSite].m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.erase(camera);

	if(m_mapInfoTemp[m_strSite].m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.empty())
	{
		m_cmbxRoom.DeleteString(m_cmbxRoom.FindString(-1,m_strRoom));
		m_cmbxRoom.SetCurSel(m_cmbxRoom.FindString(-1,"none"));

		m_mapInfoTemp[m_strSite].m_Buildings[m_strBuilding].m_Rooms.erase(m_strRoom);
		if(m_mapInfoTemp[m_strSite].m_Buildings[m_strBuilding].m_Rooms.empty())
		{
			m_cmbxBuilding.DeleteString(m_cmbxBuilding.FindString(-1,m_strBuilding));
			m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindString(-1,"none"));

			m_mapInfoTemp[m_strSite].m_Buildings.erase(m_strBuilding);
			if(m_mapInfoTemp[m_strSite].m_Buildings.empty())
			{
				m_cmbxSite.DeleteString(m_cmbxSite.FindString(-1,m_strSite));
				m_cmbxSite.SetCurSel(m_cmbxSite.FindString(-1,"none"));

				m_mapInfoTemp.erase(m_strSite);
			}
		}
	}

	m_cmbxCamera.DeleteString(m_cmbxCamera.FindString(-1,camera));
	m_cmbxCamera.SetCurSel(m_cmbxCamera.FindString(-1,"none"));
	m_bEnableSubmit = TRUE;
	SetCurState(FALSE, FALSE);
}

void CEditCameraDlg::OnSubmit() 
{
	FILE* f = NULL;
	CString hp = getHomePath();
	if(hp.IsEmpty())
	{
		f = fopen("server.txt", "w+");
		if(!f)
		{
			//hp = "c:"; // default
			hp = ".";
			saveHomePath(hp);
		}
	}
	if(!f)
	{
		hp += "\\";
		hp += "server.txt";
		f = fopen(hp, "w+");
	}

	if(f)
	{
		map<CString,CSite>::const_iterator s;
		map<CString,CBuilding>::const_iterator b;
		map<CString,CRoom>::const_iterator r;
		map<CString,CInfo>::const_iterator c;

		for(s = m_mapInfoTemp.begin(); s != m_mapInfoTemp.end(); s++)
		{
			for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
			{
				for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
				{
					for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
					{
						fprintf(f, "%s %s %s %s %i %s %s %i %i %i %i %i %i\n",
								(LPCTSTR)s->first, (LPCTSTR)b->first, (LPCTSTR)r->first, (LPCTSTR)c->first,
								(int)(c->second).m_iID, (LPCTSTR)(c->second).m_strPassword,
								(LPCTSTR)(c->second).m_ip, (int)(c->second).m_port,
								(int)(c->second).m_iFrameRate, (int)(c->second).m_iQualityRange,
								(int)(c->second).m_iBitrate, (int)(c->second).m_iWidth,
								(int)(c->second).m_iHeight);
					}
				}
			}
		}

		fclose(f);
	}

	pBaseServer->UpdateInfo(m_mapInfoTemp);

	EndDialog(IDOK);
}

void CEditCameraDlg::OnFieldchangedCameraip(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if(m_bEdit)
	{
		m_bEnableSubmit = TRUE;
		SetCurState(TRUE, TRUE);
		UpdateMap();
	}
		
	*pResult = 0;
}

void CEditCameraDlg::UpdateMap()
{
	if(UpdateData(TRUE))
	{
		in_addr inet_address;
		BYTE* pbIP = (BYTE*)&inet_address.S_un.S_addr;
		m_ipCamera.GetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);

		CInfo& cam = m_mapInfoTemp[m_strSite].
						m_Buildings[m_strBuilding].
							m_Rooms[m_strRoom].
								m_Cameras[m_strCamera];
		if (m_strPassword=="") m_strPassword="none";
		cam.setPassword(m_strPassword);
		cam.setAddress(inet_address.S_un.S_addr);
		cam.m_iID = m_nCameraID;
		cam.m_port = m_nPort;
		cam.m_iFrameRate = m_nFrameRate;
		cam.m_iBitrate = m_nBitrate;
		cam.m_iQualityRange = m_nQuality;
		cam.m_iHeight = m_nHeight;
		cam.m_iWidth = m_nWidth;
	}
}

void CEditCameraDlg::OnKillfocus() 
{
	UpdateMap();
}
